/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.je.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang.StringUtils;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;

import com.erp.ledger.je.dao.GLJeHeadDao;
import com.erp.ledger.je.dao.model.GLJeHead;
import com.erp.ledger.je.dao.model.GLJeHeadCO;

@Repository
public class GLJeHeadDaoImpl implements GLJeHeadDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(GLJeHead obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(GLJeHead obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(GLJeHead obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(GLJeHead obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<GLJeHead> getDataObjects() {
        return this.daoSupport.getDataAllObject(GLJeHead.class);
    }

    @Override
    public GLJeHead getDataObject(int id) {
        return (GLJeHead)this.daoSupport.getDataObject(GLJeHead.class, id);
    }
    
    @Override
    public GLJeHead getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<GLJeHead> getDataObjects(GLJeHeadCO paramObj) {
        return null;
    }
    
    @Override
    public List<GLJeHead> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<GLJeHead> getDataObjects(Pages pages, GLJeHeadCO paramObj) {
        String sql = "select v.* from gl_je_head v where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "jeHeadCode", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "jeName", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "jeType", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "jeSource", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "periodName", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "postStatus", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and v.", args);
        sql = sql + DaoCRUDUtil.getSQLSearchDT(paramObj, "jeDate", "jeStartDate", "jeEndDate", "and v.", args);
        
        sql = sql + " order by v.je_head_id desc";
        
        this.daoSupport.validateDataObject(paramObj);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("v", GLJeHead.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, GLJeHeadCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<GLJeHead> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, GLJeHeadCO paramObj) {
        return null;
    }
    
    @Override
    public int getHeadNum(String startDate, String endDate) {
        String sql = "select count(*) from gl_je_head where created_date between :startDate and :endDate";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("startDate", startDate);
        args.put("endDate", endDate);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list.size()>0) {
            return this.daoSupport.convertSQLCount(list.get(0));
        }
        
        return 0;
    }
    
    @Override
    public void updateHeadForApproveStatus(Integer headId, String approveStatus) {
        String sql = "update gl_je_head set approve_status = :approvestatus ";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("approvestatus", approveStatus);
        args.put("headid", headId);
        
        if(approveStatus.equals("APPROVE")) {
            sql = sql + " ,status = 'CONFIRM'";
        }
        
        sql = sql + " where je_head_id = :headid";
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public void updateHeadForStatus(Integer headId, String status) {
        String sql = "update gl_je_head set status = :status where je_head_id = :headid";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("status", status);
        args.put("headid", headId);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public void updateJePostStatus(Integer headId) {
        String sql = "update gl_je_head set post_status = 'Y' where je_head_id = :headId";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("headId", headId);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public List<GLJeHead> getGLJeHeadPostListByIds(String headIds) {
        String sql = "select h.* from gl_je_head h where h.approve_status = 'APPROVE' and h.post_status = 'N' and h.je_head_id in ("+headIds+") order by h.je_date, h.je_head_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("h", GLJeHead.class);
        
        return this.daoSupport.selectDataSql(sql, entity);
    }
    
    @Override
    public List<GLJeHead> getGLJeHeadPostListByPeriodName(String periodName) {
        String sql = "select h.* from gl_je_head h where h.approve_status = 'APPROVE' and h.post_status = 'N' and h.period_name = :periodName order by h.je_date, h.je_head_id";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("periodName", periodName);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("h", GLJeHead.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public int getJeNumByStatus(String status, String approveStatus, String postStatus, String periodName) {
        String sql = "select count(*) from gl_je_head "
                + "where status = :status and approve_status  = :approveStatus and post_status = :postStatus "
                + "and period_name = :periodName";
     
         Map<String, Object> args = new HashMap<String, Object>();
         args.put("status", status);
         args.put("approveStatus", approveStatus);
         args.put("postStatus", postStatus);
         args.put("periodName", periodName);
         
         List list = this.daoSupport.selectDataSqlCount(sql, args);
         if(list.size()>0) {
             return this.daoSupport.convertSQLCount(list.get(0));
         }
         
         return 0;
    }
    
}
